package org.wyf.EveryPoint.common.controller;

import org.wyf.EveryPoint.common.annotation.ControllerEndpoint;
import org.wyf.EveryPoint.common.controller.BaseController;
import org.wyf.EveryPoint.common.entity.FebsConstant;
import org.wyf.EveryPoint.common.entity.FebsResponse;
import org.wyf.EveryPoint.common.entity.QueryRequest;
import org.wyf.EveryPoint.common.utils.FebsUtil;
import org.wyf.EveryPoint.common.entity.DictItem;
import org.wyf.EveryPoint.common.service.IDictItemService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 字典表 Controller
 *
 * @author wyf
 * @date 2021-04-21 15:46:11
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class DictItemController extends BaseController {

    private final IDictItemService dictItemService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "dictItem")
    public String dictItemIndex(){
        return FebsUtil.view("dictItem/dictItem");
    }

    @GetMapping("dictItem")
    @ResponseBody
    @RequiresPermissions("dictItem:list")
    public FebsResponse getAllDictItems(DictItem dictItem) {
        return new FebsResponse().success().data(dictItemService.findDictItems(dictItem));
    }

    @GetMapping("dictItem/list")
    @ResponseBody
    @RequiresPermissions("dictItem:list")
    public FebsResponse dictItemList(QueryRequest request, DictItem dictItem) {
        Map<String, Object> dataTable = getDataTable(this.dictItemService.findDictItems(request, dictItem));
        return new FebsResponse().success().data(dataTable);
    }

    @ControllerEndpoint(operation = "新增DictItem", exceptionMessage = "新增DictItem失败")
    @PostMapping("dictItem")
    @ResponseBody
    @RequiresPermissions("dictItem:add")
    public FebsResponse addDictItem(@Valid DictItem dictItem) {
        this.dictItemService.createDictItem(dictItem);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "删除DictItem", exceptionMessage = "删除DictItem失败")
    @GetMapping("dictItem/delete")
    @ResponseBody
    @RequiresPermissions("dictItem:delete")
    public FebsResponse deleteDictItem(DictItem dictItem) {
        this.dictItemService.deleteDictItem(dictItem);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改DictItem", exceptionMessage = "修改DictItem失败")
    @PostMapping("dictItem/update")
    @ResponseBody
    @RequiresPermissions("dictItem:update")
    public FebsResponse updateDictItem(DictItem dictItem) {
        this.dictItemService.updateDictItem(dictItem);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改DictItem", exceptionMessage = "导出Excel失败")
    @PostMapping("dictItem/excel")
    @ResponseBody
    @RequiresPermissions("dictItem:export")
    public void export(QueryRequest queryRequest, DictItem dictItem, HttpServletResponse response) {
        List<DictItem> dictItems = this.dictItemService.findDictItems(queryRequest, dictItem).getRecords();
        ExcelKit.$Export(DictItem.class, response).downXlsx(dictItems, false);
    }
}
